import { _cartConfirmOrder, _orderSubmit } from "../../../service/api";

Page({
  data: {
    addressId: 0, // 地址ID
    checkedAddress: {}, // 地址信息
    checkedGoodsList: [], // 商品列表
    freightPrice: 0, // 快递费
    goodsTotalPrice: 0, // 商品总价
    orderTotalPrice: 0, // 订单总价
    actualPrice: 0, // 实际需要支付的总价
  },
  onLoad: function (options) {
    const addressId = wx.getStorageSync("addressId") || 0;
    this.setData({ addressId });
    this.getOrderInfo();
  },
  onShow() {},
  async getOrderInfo() {
    const res = await _cartConfirmOrder({ addressId: this.data.addressId });
    this.setData({
      addressId: res.checkedAddress.id,
      actualPrice: res.actualPrice,
      checkedAddress: res.checkedAddress,
      checkedGoodsList: res.checkedGoodsList,
      freightPrice: res.freightPrice,
      goodsTotalPrice: res.goodsTotalPrice,
      orderTotalPrice: res.orderTotalPrice,
    });
  },
  // 选择地址
  handleChangeAddress() {
    wx.navigateTo({
      url: "/pages/shopping/address/address",
    });
  },
  // 添加地址
  handleNewAddress() {
    wx.navigateTo({
      url: "/pages/shopping/new_address/new_address",
    });
  },
  handleSubmitOrder() {
    if (this.data.addressId === 0) {
      wx.showToast({
        title: "请选择收货地址",
        icon: "error",
        duration: 1000,
        mask: true,
      });
    }
    _orderSubmit({ addressId: this.data.addressId }).then((res) => {
      console.log(11);
      if (res.errno === 200) {
        wx.redirectTo({
          url: "/pages/pay_result/pay_result?orderId=" + res.orderId,
        });
      } else {
        wx.showToast({
          title: "下单失败",
          icon: "error",
          duration: 1000,
          mask: true,
        });
      }
    });
  },
});
